<template>
  <a-modal
    title="进展情况"
    :visible="visible"
    :confirm-loading="loading"
    :width="800"
    @ok="submit"
    @cancel="cancel()"
  >
    <a-spin :spinning="loading">
      <a-form-model
        ref="form"
        :model="formData"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        :rules="rules"
      >
        <a-form-model-item
          label="进展情况内容"
          prop="promotionMatters"
        >
          <BaseTextarea
            v-model.trim="formData.promotionMatters"
            placeholder="请输入"
          />
        </a-form-model-item>
        <a-form-model-item label="附件">
          <l-table
            v-if="visible"
            :upload-arrys.sync="formData.fileInfoList"
            :business-code="businessCode"
          />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { addUpdateLawmanageProgress, editUpdateLawmanageProgress } from '@/api/legal'
import { addProgress, editProgress } from '@/api/legal/caseAndLetter/caseManage.js'
export default {
  name:'a'+Date.now(),
  props: {
    visible: {
      type: Boolean,
      default: () => false
    },
    form: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {},
      loading: false,
      businessCode: 'LCM05',
      labelCol: { span: 3 },
      wrapperCol: { span: 21 },
      rules: {
        promotionMatters: [{ required: true, message: '请输入进展情况内容', trigger: 'blur' }],
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.formData = this.$clone(this.form)
      } else {
        this.formData = {}
      }
    }
  },
  methods: {
    cancel() {
      this.$refs.form.resetFields()
      this.$emit('cancel')
    },
    /**
     * 更新进展
     **/
    submit() {
      this.$refs.form.validate(async (valid) => {
        try {
          if (!valid) return this.$message.warning('信息不完整')
          if (this.loading) return
          this.loading = true
          const { id, executionId, promotionMatters, fileInfoList} = this.formData
          const params = {
              id,
              executionId,
              process: {
                  fileInfoList: fileInfoList && fileInfoList.map(item => item.commonId) || [],
                  promotionMatters: promotionMatters,
              }
          }
          const fn = id ? editProgress : addProgress
          await fn(params)
          this.$message.success('保存成功')
          this.$emit('ok')
        } catch (e) {
          this.$message.warning('保存失败')
        }
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>
</style>